package com.wtwd.campus.receiver;

import com.wtwd.campus.entity.MqLocationData;
import com.wtwd.campus.entity.MqSOSUpMessage;
import com.wtwd.campus.service.IRMFeignClientService;
import com.wtwd.campus.service.InjectFeignService;
import com.wtwd.campus.utils.LocationUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

public class UploadSOSWarningReceiver implements Receiver {
    @Autowired
    private IRMFeignClientService irmFeignClientService;

    @Override
    public String parseMessage(String data) {
        // *WT,866248050395985,SOS,100444,V,0.0000,S,0.0000,W,0.00,0,460,1,102264330,9519,5,0c:4b:54:3e:6d:5b,-43,3c:46:d8:87:96:f1,-54,c2:d5:4d:3b:2c:03,-68,28:94:0f:f9:c4:85,-68,38:83:45:cc:7d:62,-85,040621,FFDFFFFF#
        //分割字符串获取指令信息
        String[] split = data.split(",");
        String imei = split[1];
        String messageType = split[2];
        String commandNumber = split[3];
        String lat = split[5];
        String lng = split[7];
        String speed = split[9];
        String orientation = split[10];
        String mcc = split[11];//移动设备国家代码
        String nid = split[12];//网络识别码
        String sid = split[13];//系统识别码
        String cellId = split[14];
        int wifiNum = Integer.parseInt(split[15]);
        String wifiInfo = "";
        //wifi信息:mac地址,信号强度,wifi名称|mac地址,信号强度,wifi名称
        for (int i = 0; i < wifiNum; i++) {
            if(StringUtils.isNotBlank(wifiInfo)){
                wifiInfo =wifiInfo.concat("|");
            }
            wifiInfo =wifiInfo.concat(split[16 + 2*i]).concat(",").concat(split[17 + 2*i]);
        }
        String date = split[split.length-2];//日月年
        String deviceStatus = split[split.length-1];
        MqSOSUpMessage mqSOSUpMessage = new MqSOSUpMessage();
        mqSOSUpMessage.setCommandNumber(commandNumber);
        mqSOSUpMessage.setImei(imei);
        mqSOSUpMessage.setDeviceDate(date);
        mqSOSUpMessage.setDeviceTime(commandNumber);
        mqSOSUpMessage.setWifiInfo(wifiInfo);
        MqLocationData mqLocationData = new MqLocationData();
        List<Double> list = LocationUtils.transiformLatAndLng(lng, lat);
        mqLocationData.setOrientation(Integer.parseInt(orientation));
        mqLocationData.setSpeed(Double.parseDouble(speed));
        mqLocationData.setLatitude(list.get(1));
        mqLocationData.setLongitude(list.get(0));
        mqSOSUpMessage.setMqLocationData(mqLocationData);
        InjectFeignService.getIrmFeignClientService().upSOSMessage(mqSOSUpMessage);
        return imei;
//        if (date.length() < 3) {
//            //有wifi数据
//            int wifiNum = Integer.parseInt(date);
//            //wifi数据暂不处理
//            currentIndex += wifiNum * 2;
//            date = split[currentIndex];
//            currentIndex++;
//        }
//        String deviceStatus = split[currentIndex];//设备状态

        //构建一个存储一般定位信息和一般设备信息的实体类对象
//        MqNormalData mqNormalData = new MqNormalData();
//        mqNormalData.setCommandNumber(commandNumber);
//        mqNormalData.setImei(imei);
//        mqNormalData.setTime(time);
//        mqNormalData.setSpeed(Double.parseDouble(speed));
//        mqNormalData.setLongitude(list.get(0));
//        mqNormalData.setLatitude(list.get(1));
//        mqNormalData.setOrientation(Integer.parseInt(orientation));
//        mqNormalData.setDate(date);
//        mqNormalData.setDeviceStatus(convertDeviceStatus(deviceStatus));
//        mqNormalData.setDataValid(dataValid);
//        mqNormalData.setLatNS(latNS);
//        mqNormalData.setLngWE(lngWE);
//        mqNormalData.setMcc(mcc);
//        mqNormalData.setNid(nid);
//        mqNormalData.setSid(sid);
//        mqNormalData.setCellId(cellId);
//        //构建一个存储相关信息的实体类对象
//        MqLocationData mqLocationData = new MqLocationData();
//        mqLocationData.setMqNormalData(mqNormalData);
//        //远程调用，
//        InjectFeignService.getIrmFeignClientService().uploadSOSWarningData(mqLocationData);
    }
}
